﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using MediaPortal.Configuration;
using MediaPortal.Profile;

namespace InfoService.GUIConfiguration
{
    public partial class AdvancedConfigForm : Form
    {
        public AdvancedConfigForm()
        {
            InitializeComponent();
        }

        private void btnTwitterDeleteCache_Click(object sender, EventArgs e)
        {
            string twitterDir = Config.GetFolder(Config.Dir.Thumbs) + @"\InfoService\Twitter\";
            if(Directory.Exists(twitterDir))
            {
                try
                {
                    Directory.Delete(twitterDir, true);
                    MessageBox.Show(this, "Twitter cache cleared successfully.", "Twitter cache", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "An error occured while clearing the twitter cache.\n\n" + ex.Message, "Twitter cache", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            else MessageBox.Show(this, "Twitter cache is already cleared.", "Twitter cache", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnFeedDeleteCache_Click(object sender, EventArgs e)
        {
            string feedsDir = Config.GetFolder(Config.Dir.Thumbs) + @"\InfoService\Feeds\";
            if(Directory.Exists(feedsDir))
            {
                try
                {
                    Directory.Delete(feedsDir, true);
                    MessageBox.Show(this, "Feed cache cleared successfully.", "Feed cache", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "An error occured while clearing the feed cache.\n\n" + ex.Message, "Feed cache", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
            }
            else MessageBox.Show(this, "Feed cache is already cleared.", "Feed cache", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
            {
                settings.SetValue("InfoService", "FeedDeletionInterval", coFeedDeleteCache.Text);
                settings.SetValue("InfoService", "TwitterDeletionInterval", coTwitterDeleteCache.Text);

                settings.SetValueAsBool("InfoService", "LogWarning", cbWarning.Checked);
                settings.SetValueAsBool("InfoService", "LogError", cbError.Checked);
                settings.SetValueAsBool("InfoService", "LogDebug", cbDebug.Checked);

                settings.SetValue("InfoService", "PluginName", txtPluginName.Text);
            }
            Close();
        }

        private void AdvancedConfigForm_Load(object sender, EventArgs e)
        {
            using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
            {
                coFeedDeleteCache.Text = settings.GetValueAsString("InfoService", "FeedDeletionInterval", "Week");
                coTwitterDeleteCache.Text = settings.GetValueAsString("InfoService", "TwitterDeletionInterval", "Day");

                cbWarning.Checked = settings.GetValueAsBool("InfoService", "LogWarning", true);
                cbError.Checked = settings.GetValueAsBool("InfoService", "LogError", true);
                cbDebug.Checked = settings.GetValueAsBool("InfoService", "LogDebug", false);

                txtPluginName.Text = settings.GetValueAsString("InfoService", "PluginName", "InfoService");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOpenLogFolder_Click(object sender, EventArgs e)
        {
            string dir = Config.GetFolder(Config.Dir.Log);
            if (Directory.Exists(dir))
            {
                Process.Start(dir);
            }
            else
            {
                MessageBox.Show("Error opening " + dir + ". Directory doesn't exist. It seems that the are some MediaPortal configuration problems.", "Error opening folder!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnOpenLog_Click(object sender, EventArgs e)
        {
            string logFile = Config.GetFolder(Config.Dir.Log) + @"\infoservice.log";
            if (File.Exists(logFile))
            {
                Process.Start(logFile);
            }
            else
            {
                MessageBox.Show("Error opening " + logFile + ". The log file doesn't exist. Please start MediaPortal first, to show the log file.", "Error opening file!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void AdvancedConfigForm_HelpButtonClicked(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Process.Start(@"http://infoservice.codeplex.com/wikipage?title=Advanced%20configuration&referringTitle=How%20to%20use%20%28Users%29");
        }
    }
}
